<template>
  <div>
    <el-button @click="show_dialog">新增</el-button>
    <!-- 文章列表 -->
    <el-table :data="hospital.list" style="width: 100%">
      <el-table-column prop="name" label="医院名称" align="center">
      </el-table-column>
      <el-table-column prop="introduce" label="医院简介" align="center">
      </el-table-column>
      <el-table-column prop="address" label="医院地址" align="center">
      </el-table-column>
      <el-table-column prop="flags" label="标记" align="center">
      </el-table-column>
      <el-table-column prop="longitude" label="经度" align="center">
      </el-table-column>
      <el-table-column prop="latitude" label="纬度" align="center">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <span style="margin-right: 5px" @click="delete_hospital(scope.row.id)"
            >删除</span
          >
          <span @click="edit_hospital(scope.row)">修改</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      background
      layout="total, prev, pager, next"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      @current-change="change"
      :total="hospital.total"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialog_hospital" width="80%">
      {{ form_hospital }}
      <el-form :model="form_hospital">
        <el-form-item label="医院名称" :label-width="formLabelWidth_hospital">
          <el-input v-model="form_hospital.name"></el-input>
        </el-form-item>
        <el-form-item label="医院简介" :label-width="formLabelWidth_hospital">
          <el-input v-model="form_hospital.introduce"></el-input>
        </el-form-item>
        <el-form-item label="省市区" :label-width="formLabelWidth_hospital">
          <el-cascader
            v-model="form_hospital.pca"
            :options="pca"
            :props="{ expandTrigger: 'hover', label: 'name', value: 'name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth_hospital">
          <el-input v-model="form_hospital.address"></el-input>
        </el-form-item>
        <el-form-item label="标记" :label-width="formLabelWidth_hospital">
          <el-input v-model="form_hospital.flags"></el-input>
        </el-form-item>
        <el-form-item label="经度" :label-width="formLabelWidth_hospital">
          <el-input disabled v-model="form_hospital.longitude"></el-input>
        </el-form-item>
        <el-form-item label="纬度" :label-width="formLabelWidth_hospital">
          <el-input disabled v-model="form_hospital.latitude"></el-input>
        </el-form-item>
        <el-form-item label="选择经纬度" :label-width="formLabelWidth_hospital">
          <el-button @click="show_map">打开地图</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_hospital = false">取 消</el-button>
        <el-button type="primary" @click="savehospital">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择地点" :visible.sync="dialog_map" width="80%">
      <div id="Map" style="height:400px;"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog_map = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
import pca from '../../assets/pca.json'
export default {
  computed: {
    ...mapState("hospital", ["hospital"]),
  },
  data() {
    return {
      pca,
      obj: {
        page: 1,
        pageSize: 10,
      },
      title: "新增医院",
      dialog_hospital: false,
      form_hospital: {},
      formLabelWidth_hospital: "120px",
      dialog_map:false,
      map:{}
    };
  },
  created() {
    this.findHos(this.obj);
  },
  methods: {
    ...mapActions("hospital", ["findHos","saveHos"]),
    show_dialog() {
      this.dialog_hospital = true;
    },
    delete_hospital() {},
    edit_hospital() {},
    change() {},
    savehospital() {
        this.form_hospital.province = this.form_hospital.pca[0];
        this.form_hospital.city = this.form_hospital.pca[1];
        this.form_hospital.area = this.form_hospital.pca[2];
        this.saveHos(this.form_hospital).then((res)=>{
           this.dialog_hospital = false;
           this.findHos(this.obj);
        });
        console.log(this.form_hospital);
    },
    handleChange(){},
    show_map(){
      this.dialog_map = true;
      setTimeout(()=>{
        this.initMap();
        this.clickOn();
      },1000)
      
    },
    // 初始化地图
    initMap(){
      this.map = new AMap.Map("Map", {
        resizeEnable: true
    });
    // console.log(map);
    },
    showInfoClick(e){
      this.form_hospital.longitude = e.lnglat.getLng();
      this.form_hospital.latitude = e.lnglat.getLat();
        // e.lnglat.getLng()
        // e.lnglat.getLat()
        console.log(e.lnglat.getLng(),e.lnglat.getLat());  
    },
    clickOn(){
        this.map.on('click', this.showInfoClick);
    }
  },
};
</script>